package com.lihui.exercises;

import com.lihui.dao.CourseDao;
import com.lihui.dao.ScoreDao;
import com.lihui.dao.StudentDao;
import com.lihui.model.Course;
import com.lihui.model.Score;
import com.lihui.model.Student;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;
import java.util.Scanner;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2022/12/29  16:38
 */
public class Exe_05 {
    public static void main(String[] args) {
        while(true){
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入学生姓名：");
            String studentName = scanner.nextLine();
            if(studentName == null || studentName.equals("")){
                System.out.println("输入的名字为空 -->" + studentName);
                return;
            }
            StudentDao studentDao = new StudentDao();
            List<Student> students = studentDao.selectByName(studentName);
            if(students == null || students.size() == 0){
                System.out.println("没有该学生信息 -->" + studentName);
                return;
            }
            for (Student student: students) {
                String info = MessageFormat.format("编号: {0}\t| 学号: {1}\t| 姓名: {2}\t | " +
                                "邮箱: {3}\t| 班级编号: {4}", student.getId(), student.getSn(), student.getName(),
                        student.getMail(), student.getClassesId());
                System.out.println(info);
            }

            System.out.println("请输入课程名称：");
            String courseName = scanner.nextLine();
            if(courseName == null || courseName.equals("")){
                System.out.println("输入的课程名为空 -->" + courseName);
                return;
            }
             CourseDao courseDao = new CourseDao();
            List<Course> courses = courseDao.selectByName(courseName);
            if(courses == null || courses.size() == 0){
                System.out.println("没有该课程信息 -->" + courseName);
                return;
            }

            for (Course course:courses) {
                String info = MessageFormat.format("编号: {0}\t| 名称: {1} "
                                 , course.getId(), course.getName());
                System.out.println(info);
            }

            System.out.println("请输入学生编号：");
            int S_id = scanner.nextInt();
            System.out.println("请输入课程编号：");
            int C_id = scanner.nextInt();
            System.out.println("请输入分数：");
            BigDecimal score = new BigDecimal(scanner.next());
            // 打印日志
            System.out.println(S_id + "-" + C_id + "-" + score);
            // 构建Score对象
            Score scoreObj = new Score();
            scoreObj.setScore(score);
            scoreObj.setStudentId(S_id);
            scoreObj.setCourseId(C_id);
            // 调和ScoreDao完成插入操作
            ScoreDao scoreDao = new ScoreDao();
            scoreDao.insert(scoreObj);
        }
    }
}
